// allreview.js  全部评价
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    page_size: 20,
    listflag: true,
    has_more: true,
    review_list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getReview(options.id)
  },
  previewImg: function(e) {
    var commentidx = e.currentTarget.dataset.commentidx,
      imgidx = e.currentTarget.dataset.imgidx,
      imgs = this.data.review_list[commentidx].sh_images
    wx.previewImage({
      current: imgs[imgidx],
      urls: imgs
    })
  },
  getReview: function (id) {
    var that = this
    shihan.sendRequest(config.get_product_review + id + '/' + that.data.page + '-' + that.data.page_size + '.html', 'GET', {})
      .then(function (response) {
        if (response.data.code == 0 && response.data.data.sh_data.length > 0) {
          that.setData({
            listflag: true,
            has_more: true,
            product_id: id,
            page: that.data.page + 1,
            review_list: that.data.review_list.concat(response.data.data.sh_data)
          })
          if (that.data.review_list.length == response.data.data.sh_total_rows) {
            that.setData({
              listflag: false,
              has_more: false
            })
          }
        } else {
          that.setData({
            listflag: false,
            has_more: false,
          })
        }
        wx.stopPullDownRefresh()
      }, function (error) {
        console.log(error);
      });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      page: 1,
      page_size: 20,
      listflag: true,
      has_more: true,
      review_list: []
    })
    this.getReview(this.data.product_id)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.listflag) {
      this.setData({
        listflag: false
      })
      this.getReview(this.data.product_id)
    }
  }
})